use prometheus::{
    Encoder,
    TextEncoder,
    Registry,
    IntCounterVec,
    IntGaugeVec,
    register_histogram_vec,
};

#[derive(Clone)]
pub struct Metrics {
    pub register: Registry,
    pub counter: IntCounterVec,
    pub gauge: IntGaugeVec,
}

impl Metrics {
    pub fn new() -> Self {
        let register = Registry::new();
        let counter = IntCounterVec::new(
            prometheus::Opts::new(
                "my_counter",
                "This is my counter",
            ),
            &["label1", "label2"],
        ).unwrap();
        register.register(Box::new(counter.clone())).unwrap();

        let gauge = IntGaugeVec::new(   
            prometheus::Opts::new(
                "my_gauge",
                "This is my gauge",
            ),
            &["label1", "label2"],
        ).unwrap();
        register.register(Box::new(gauge.clone())).unwrap();
        Self {
            register,
            counter,
            gauge,
        }
    }
    pub fn inc_counter(&self, label1: &str, label2: &str) {
        self.counter.with_label_values(&[label1, label2]).inc();
    }
    pub fn set_guage(&self, label1: &str, label2: &str) {
    self.gauge.with_label_values(&[label1, label2]).set(10);
    self.gauge.with_label_values(&[label1, label2]).sub(2);
}
}

pub async fn metrics_handler2(metrics: Metrics) -> String{
     let encoder = TextEncoder::new();
    let metric_families = metrics.register.gather();
    let mut buffer = Vec::new();
    encoder.encode(&metric_families, &mut buffer).unwrap();
    String::from_utf8(buffer).unwrap()
}